package com.gw.booking.push;


import com.gw.booking.util.ConfigProperties;

public class Push {
    private PushClient client = new PushClient();

    /**
     * 后台广播发送
     *
     * @param title
     * @param text
     * @param type
     * @param url
     */
    public void sendBackendBroadcast(String title, String text, String type, String url) {
        try {
            AndroidBroadcast androidBroadcast = new AndroidBroadcast(ConfigProperties.getBusinessAndroidAppKey(),
                    ConfigProperties.getBusinessAndroidMasterSecret());
            sendAndroidBroadcast(androidBroadcast, title, text, type, url);
            IOSBroadcast iosBroadcast = new IOSBroadcast(ConfigProperties.getRestaurantIOSAppKey(),
                    ConfigProperties.getRestaurantIOSMasterSecret());
            sendIOSBroadcast(iosBroadcast, title, text, type, url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void sendAndroidBroadcast(AndroidBroadcast broadcast, String title, String text,
                                      String type, String url) throws Exception {
        broadcast.setTicker(title);
        broadcast.setTitle(title);
        broadcast.setText(text);
        // broadcast.goAppAfterOpen();
        broadcast.setAfterOpenAction(AndroidNotification.AfterOpenAction.go_custom);
        broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        broadcast.setProductionMode();
        // broadcast.setTestMode();
        // Set customized fields
        broadcast.setExtraField("title", title);
        broadcast.setExtraField("content", text);
        broadcast.setExtraField("pushType", type);
        broadcast.setExtraField("url", url);
        client.send(broadcast);
    }

    private void sendIOSBroadcast(IOSBroadcast iosBroadcast, String title, String text, String type,
                                  String url) throws Exception {
        iosBroadcast.setAlert(text);
        iosBroadcast.setBadge(0);
        iosBroadcast.setSound("default");
//        iosBroadcast.setDescription(description);
        // TODO set 'production_mode' to 'true' if your app is under
        // production mode
        iosBroadcast.setProductionMode();
        // iosBroadcast.setTestMode();
        iosBroadcast.setCustomizedField("title", title);
        iosBroadcast.setCustomizedField("content", text);
        iosBroadcast.setCustomizedField("pushType", type);
        iosBroadcast.setCustomizedField("url", url);
        client.send(iosBroadcast);
    }

    /**
     * 商家版单播发送
     *
     * @param deviceToken
     * @param title
     * @param text
     * @param type
     * @param linkId
     */
    public void sendBusinessUnicast(String deviceToken, String title, String text, String type, String linkId) {
        try {
            // 安卓
            if (44 == deviceToken.length()) {
                AndroidUnicast unicast = new AndroidUnicast(ConfigProperties.getBusinessAndroidAppKey(),
                        ConfigProperties.getBusinessAndroidMasterSecret());
                sendAndroidUnicast(unicast, deviceToken, title, text, type, linkId);
                // IOS
            } else if (64 == deviceToken.length()) {
                IOSUnicast unicast = new IOSUnicast(ConfigProperties.getBusinessIOSAppKey(),
                        ConfigProperties.getBusinessIOSMasterSecret());
                sendIOSUnicast(unicast, deviceToken, text, type, linkId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 酒店版单播发送
     *
     * @param deviceToken
     * @param title
     * @param text
     * @param type
     * @param linkId
     */
    public void sendRestaurantUnicast(String deviceToken, String title, String text, String type, String linkId) {
        try {
            // 安卓
            if (44 == deviceToken.length()) {
                AndroidUnicast unicast = new AndroidUnicast(ConfigProperties.getRestaurantAndroidAppKey(),
                        ConfigProperties.getRestaurantAndroidMasterSecret());
                sendAndroidUnicast(unicast, deviceToken, title, text, type, linkId);
                // IOS
            } else if (64 == deviceToken.length()) {
                IOSUnicast unicast = new IOSUnicast(ConfigProperties.getRestaurantIOSAppKey(),
                        ConfigProperties.getRestaurantIOSMasterSecret());
                sendIOSUnicast(unicast, deviceToken, text, type, linkId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void sendAndroidUnicast(AndroidUnicast unicast, String deviceToken, String title, String text, String type,
                                    String linkId) throws Exception {
        unicast.setDeviceToken(deviceToken);
        unicast.setTicker(title);
        unicast.setTitle(title);
        unicast.setText(text);
        unicast.goAppAfterOpen();
        unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        unicast.setProductionMode(); // 生产环境
        // unicast.setTestMode(); // 测试环境
        unicast.setExtraField("type", type);
        unicast.setExtraField("linkId", linkId);
        client.send(unicast);
    }

    private void sendIOSUnicast(IOSUnicast unicast, String deviceToken, String text, String type, String linkId)
            throws Exception {
        unicast.setDeviceToken(deviceToken);
        unicast.setAlert(text);
        unicast.setBadge(0);
        unicast.setSound("default");
        unicast.setProductionMode(); // 生产环境
        // unicast.setTestMode(); // 测试环境
        unicast.setCustomizedField("type", type);
        unicast.setCustomizedField("linkId", linkId);
        client.send(unicast);
    }
}
